package com.intpro.demo.Service;

import com.intpro.demo.DAO.AdminUser;
import com.intpro.demo.Utils.AESToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class AdminService
{

	@Autowired
	MongoTemplate mongoTemplate;

	public AdminUser getUserById(long id)
	{
		Query q1 = new Query(Criteria.where("id").is(id));
		return mongoTemplate.findOne(q1, AdminUser.class);
	}
	public void addAdmin(AdminUser u)
	{
		mongoTemplate.save(u);
	}
	public AdminUser checkPswByAccount(String account, String psw) throws Exception
	{
		String t = psw;//AESToken.encrypt(psw);
		Query q1 = new Query(Criteria.where("account").is(account));
		AdminUser u = mongoTemplate.findOne(q1, AdminUser.class);
		if (u == null)
		{
			return null;
		}
		if (!u.password.equals(t))
		{
			u.id = -1;
		}
		return u;
	}
}
